﻿using System;
using System.Data.SqlClient;
using Devv.Core.UniDataTier;
using Yglu.BusinessLayer.Entities;

namespace Yglu.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader Promotion_Get()
		{
			DataHandler db = new DataHandler();

			return (SqlDataReader)db.ExecuteReader("upPromotion_Get");
		}

		public static SqlDataReader Promotion_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@PromotionId", id);

			return (SqlDataReader)db.ExecuteReader("upPromotion_Get");
		}

		public static SqlDataReader Promotion_Get(Store store)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@StoreId", store.Id);

			return (SqlDataReader)db.ExecuteReader("upPromotion_Get");
		}

		#endregion

		#region Insert and update

		public static void Promotion_Upsert(Promotion promotion)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@Title", promotion.Title);
				db.AddParameter("@Discount", promotion.Discount);
				db.AddParameter("@IsPercent", promotion.IsPercent);
				db.AddParameter("@Automatic", promotion.Automatic);
				db.AddParameter("@AllStores", promotion.AllStores);
				db.AddParameter("@Active", promotion.Active);

				if (promotion.Products > 0)
				{
					db.AddParameter("@Products", promotion.Products);
				}

				if (promotion.Combos > 0)
				{
					db.AddParameter("@Combos", promotion.Combos);
				}

				if (promotion.StoreId > 0)
				{
					db.AddParameter("@StoreId", promotion.StoreId);
				}

				if (promotion.Id > 0)
				{
					db.AddParameter("@PromotionId", promotion.Id);
					db.ExecuteScalar("upPromotion_Update");
				}
				else
				{
					promotion.Id = Convert.ToInt32(db.ExecuteScalar("upPromotion_Insert"));
				}
			}
		}

		#endregion
	}
}